package com.xana.acg.mikomiko.actis;


import android.os.Build;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.Toast;

import com.xana.acg.com.app.Activity;
import com.xana.acg.mikomiko.R;
import com.xana.acg.mikomiko.media.AnimeView;
import com.xana.acg.mikomiko.media.EpisodePopup;
import com.xana.acg.mikomiko.media.VideoSpeedPopup;
import com.xana.acg.mikomiko.views.AnimView;

import butterknife.BindView;
import cn.jzvd.Jzvd;

public class VideoPlayerActivity extends Activity implements
        VideoSpeedPopup.SpeedChangeListener,
        AnimeView.JzVideoListener {
    @BindView(R.id.jz_video)
    AnimView jz_video;


    //倍数弹窗
    private VideoSpeedPopup videoSpeedPopup;
    private EpisodePopup episodePopup;


    @Override
    protected int getLayoutId() {
        return R.layout.view_player;
    }

    @Override
    protected void initWidget() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
        }
        super.initWidget();
        jz_video.setJzVideoListener(this);
    }
    String uri = "https://1251316161.vod2.myqcloud.com/5f6ddb64vodsh1251316161/5e8fb66d5285890814932760236/hFaTX5rTVmEA.mp4";
    String url = "https://d1--cn-gotcha04.bilivideo.com/live-bvc/940414/live_269415357_9827682_1500.flv?cdn=cn-gotcha04&expires=1626677361&len=0&oi=1883946817&pt=android&qn=150&trid=1000fe4efbf90ef44d5dbe2c931263213edf&sigparams=cdn,expires,len,oi,pt,qn,trid&sign=3d78863cff3f3e0b9bfefd4128a765c7&ptype=0&src=9&sl=2&sk=417e709c171a500&order=1";
    String live = "rtmp://112.74.191.65/myapp/mystream";
    @Override
    protected void initData() {
        super.initData();
        jz_video.setUp(uri, "转生史莱姆");
        jz_video.posterImageView.setImageResource(R.drawable.bg_ekidora);
    }

    @Override
    public void nextClick() {

    }

    @Override
    public void backClick() {
        onBackPressed();
    }

    @Override
    public void throwingScreenClick() {

    }

    @Override
    public void selectPartsClick() {
        if (episodePopup == null) {
            episodePopup = new EpisodePopup(this);
//            episodePopup.setEpisondeClickListener(this);
        }
//        videoEpisodePopup.setPlayNum(episodes.getSelectedTabPosition() + 1);
        episodePopup.showAtLocation(getWindow().getDecorView(), Gravity.RIGHT, 0, 0);
    }

    @Override
    public void speedClick() {
        if (videoSpeedPopup == null) {
            videoSpeedPopup = new VideoSpeedPopup(this);
            videoSpeedPopup.setSpeedChangeListener(this);
        }
        videoSpeedPopup.showAtLocation(getWindow().getDecorView(), Gravity.RIGHT, 0, 0);
    }

    @Override
    public void onBackPressed() {
        if (jz_video.backPress()||videoSpeedPopup!=null) {
            videoSpeedPopup.dismiss();
            return;
        }
        super.onBackPressed();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        jz_video.releaseAllVideos();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.goOnPlayOnPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Jzvd.goOnPlayOnResume();
    }

    @Override
    public void speedChange(float speed) {
        changeSpeed(speed);
    }

    /**
     * 改变播放倍速
     *
     * @param speed
     */
    private void changeSpeed(float speed) {
        Object[] object = { speed };
        jz_video.mediaInterface.setSpeed(speed);
        Toast.makeText(this, "正在以" + speed + "X倍速播放", Toast.LENGTH_SHORT).show();
        jz_video.speedChange(speed);
    }
}